package com.bing.bingPro.controller.tietong;

import cn.hutool.core.bean.BeanUtil;
import com.bing.bingPro.ExcelUtil;
import com.bing.bingPro.common.BaseResult;
import com.bing.bingPro.controller.dto.TTtInstallStatsProvinceDto;
import com.bing.bingPro.controller.vo.TTtInstallStatsCityVo;
import com.bing.bingPro.controller.vo.TTtInstallStatsProvinceVo;
import com.bing.bingPro.service.TTtInstallStatsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class TTtInstallStatsController {

    @Resource
    private TTtInstallStatsService tTtInstallStatsService;

    @PostMapping("/statsTable")
    public BaseResult<List<TTtInstallStatsCityVo>> statsTable(@RequestBody TTtInstallStatsProvinceDto tOrderYearDto) {
        return new BaseResult<>(this.tTtInstallStatsService.queryStatsProvince(tOrderYearDto));
    }

    @PostMapping("/exportStats")
    public void exportMaintenanceCity(HttpServletResponse response, @RequestBody TTtInstallStatsProvinceDto tOrderYearDto) throws Exception {
        String time = tOrderYearDto.getTime();
        String cityTitle = "铁通装机工单统计-市" + time;
        String provinceTitle = "铁通装机工单统计-省" + time;
        List<TTtInstallStatsCityVo> complainStatsCityVos = tTtInstallStatsService.exportStatsCity(tOrderYearDto);
        //市
        if (tOrderYearDto.getAreaType().equals("city")) {
            ExcelUtil.writeExcel(response, complainStatsCityVos, cityTitle, cityTitle, TTtInstallStatsCityVo.class);
        }
        //省
        List<TTtInstallStatsProvinceVo> provinceStats = complainStatsCityVos.stream().map(tStatsProvinceVo -> {
            TTtInstallStatsProvinceVo vo = new TTtInstallStatsProvinceVo();
            BeanUtil.copyProperties(tStatsProvinceVo, vo);
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil.writeExcel(response, provinceStats, provinceTitle, provinceTitle, TTtInstallStatsProvinceVo.class);
    }

}
